import pandas as pd

books = pd.read_csv('BX/BX-Books.csv', sep=';', error_bad_lines=False, encoding="latin-1")
books.columns = ['ISBN', 'bookTitle', 'bookAuthor', 'yearOfPublication', 'publisher', 'imageUrlS', 'imageUrlM',
                 'imageUrlL']
ratings = pd.read_csv('BX/BX-Book-Ratings.csv', sep=';', error_bad_lines=False, encoding="latin-1")
ratings.columns = ['userID', 'ISBN', 'bookRating']
print(ratings.shape)

# 删除信息为空的书籍
books = books[['ISBN', 'bookTitle', 'bookAuthor']]
books.dropna(axis=0, how='any', inplace=True)

# 选取用户评分次数大于5的数据
counts1 = ratings['userID'].value_counts()
counts1 = counts1[counts1.values >= 5]
ratings = ratings[ratings['userID'].isin(counts1.index)]

# 去除评分中的不在book里的数据
ratings = ratings[ratings['ISBN'].isin(books['ISBN'])]

# 评分大于8的才被认为喜欢
ratings = ratings[ratings.bookRating >= 8]

# 在评分选取评分次数大于30的书籍
counts2 = ratings['ISBN'].value_counts()
counts2 = counts2[counts2.values >= 30]
ratings = ratings[ratings['ISBN'].isin(counts2.index)]


# 在书籍里选取评分数大于30的书籍
books = books[books['ISBN'].isin(ratings['ISBN'])]

print(ratings.shape)
print(books.shape)

ratings.to_excel('MyFile/MyRatings.xlsx', encoding='utf-8')
books.to_excel('MyFile/MyBooks.xlsx', encoding='utf-8')
